<template>
   <div class="app-container">
     <!--查询条件开始-->
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="字典类型" prop="dictType">
        <el-select
          v-model="queryParams.dictType"
          placeholder="字典类型"
          clearable
          size="small"
          style="width:240px"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictType"
            :label="dict.dictName"
            :value="dict.dictType"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字典标签" prop="dictLable">
        <el-input
          v-model="queryParams.dictLabel"
          placeholder="请输入字典标签"
          clearable
          size="small"
          style="width:240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="字典状态"
          clearable
          size="small"
          style="width:240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

     <!--查询条件结束-->

     <!--表头按钮开始-->
       <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
    </el-row>
     <!--表头按钮结束-->

     <!--数据表格开始-->
    <el-table v-loading="loading" border :data="dictDataTableList" @selection-change="handleSelectionChnage">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字典编码" prop="dictCode" align="center" />
      <el-table-column label="字典标签" prop="dictLabel" align="center" />
      <el-table-column label="字典键值" prop="dictValue" align="center" />
      <el-table-column label="字典排序" prop="dictSort" align="center" />
      <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="备注" prop="remark" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="180" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

     <!--数据表格结束-->


     <!--分页控件开始-->
     <el-pagination
      v-show="total>0"
      :current-page="queryParams.pageNum"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
     <!--分页控件结束-->

   <!--添加修改弹出层开始-->
   <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      center
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" :disabled="true" size="small" />
        </el-form-item>
        <el-form-item label="数据标签" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入数据标签" clearable size="small" />
        </el-form-item>
        <el-form-item label="数据键值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入数据键值" clearable size="small" />
        </el-form-item>
        <el-form-item label="排序显示" prop="dictSort">
          <el-input-number v-model="form.dictSort" placeholder="请输入数据键值" clearable size="small" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              :value="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入字典备注" clearable size="small" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>

   <!--添加修改弹出层结束-->

   </div>
</template>
<script>
import {listForPage,addDictData,updateDictData,deleteDictDataByIds,getDictDataById,} from '@/api/system/dict/data'
import {getDictTypeById,selectAllDictType} from '@/api/system/dict/type'
export default {
  
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典数据表格数据
      dictDataTableList: [],
      //默认类型
      defaultDictType:'',
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 类型数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictLabel: undefined,
        dictType: undefined,
        status: undefined
      },
      // 表单数据 
      form: {},
      // 表单校验
      rules: {
        dictLabel: [
          { required: true, message: '数据标签不能为空', trigger: 'blur' }
        ],
        dictValue: [
          { required: true, message: '数据键值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  //钩子
  created(){
     //根据全局挂载的方法查询状态字典
    this.getDataByType('sys_normal_disable').then(res => {
      this.statusOptions = res.data
    })
      const dictId = this.$route.params.dictId
      getDictTypeById(dictId).then(res =>{
        //给查询条件的下拉框默认赋值
        this.defaultDictType=res.data.dictType
        this.queryParams.dictType = this.defaultDictType
      })  
      //查询所有的类型  不分页
      selectAllDictType().then(res =>{
        this.typeOptions =res.data.data
           this.getDictTypeData()
      })
    


  },
  methods:{
    //查询表格数据
    getDictTypeData(){
         this.loading = true  //打开遮罩  加载圈圈 
         listForPage(this.queryParams).then(res =>{
           this.dictDataTableList = res.data
           this.total = res.total
            this.loading = false
         })
    },
    //模糊查询
    handleQuery(){
      this.getDictTypeData()
    },
    //重置
    resetQuery(){
      this.queryParams.dictLabel= undefined
      this.queryParams.status = undefined
      this.getDictTypeData()
    },
   //打开添加弹出层
   handleAdd(){
     this.reset()
     this.title="添加字典数据"
     this.open= true
     this.form.dictType = this.defaultDictType

   },
   //打开修改弹出层
   handleUpdate(row){
     this.title="修改字典数据"
    let dictCode = row.dictCode ===undefined ? this.ids[0]:row.dictCode
      this.open=true
      this.reset()
      //根据row.ID去查  
      getDictDataById(dictCode).then(res =>{
        this.form = res.data
      })  
     
   },
   //点击删除
   handleDelete(row){
     let dictCodes = row.dictCode === undefined ? this.ids : row.dictCode
      this.loading=true
      // this.reset()
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log("dictCodes")
            deleteDictDataByIds(dictCodes).then(res =>{
            this.msgSuccess('删除成功')
            this.getDictTypeData()
            this.loading=false
      })
        }).catch(() => {
          this.msgInfo('删除已取消')
         this.loading=false
        });
     

   },
   //表格多选触发事件
   handleSelectionChnage(selection){
     this.ids=selection.map(item => item.dictCode)
     this.single = selection.length != 1 
     this.multiple = !selection.length>0

   },
   //分页页大小改变事件
   handleSizeChange(val){
     this.queryParams.pageSize = val
      this.getDictTypeData()
   },
   //分页当前页改变事件
   handleCurrentChange(val){
     this.queryParams.pageNum = val
      this.getDictTypeData()
   },
   //翻译状态
   statusFormatter(row){
     return  this.selectDictLabel(this.statusOptions,row.status)
   },
   //提交
   handleSubmit(){
      this.$refs['form'].validate((valid) => {
          if (valid) {
             this.loading=true
       if(this.form.dictCode === undefined){
        addDictData(this.form).then(res =>{
         this.msgSuccess('保存成功')
         this.open=false
         this.getDictTypeData()
          this.loading = false
        
       }).catch(error => {
         this.getDictTypeData()
         this.loading = false
       })
       }else{
         updateDictData(this.form).then(res =>{
         this.msgSuccess('修改成功')
         this.open=false
         this.getDictTypeData()
          this.loading = false
       }).catch(error => {
         this.getDictTypeData()
         this.loading = false
       })
       }
          } 
        });

   },
   //取消
   cancel(){
     this.open =false 
     this.loading = false
   },
   // 重置表单
    reset() {
      this.form = {
        dictCode: undefined,
        dictLable: undefined,
        dictValue: undefined,
        status: '0',
        dictSort:0,
        remark: undefined
      }
      this.resetForm('form')
    },


   
  },
  watch:{
    'queryParams.dictLabel':{//深度监听，可监听到对象、数组的变化
         handler(val, oldVal){
           this.queryParams.pageNum = 1
             this.getDictTypeData()
         },
         deep:true //true 深度监听
     },
     'queryParams.dictType':{//深度监听，可监听到对象、数组的变化
         handler(val, oldVal){
           this.queryParams.pageNum = 1
             this.getDictTypeData()
         },
         deep:true //true 深度监听
     },
     'queryParams.status':{//深度监听，可监听到对象、数组的变化
         handler(val, oldVal){
           this.queryParams.pageNum = 1
             this.getDictTypeData()
         },
         deep:true //true 深度监听
     }
    }


  }



</script>